<template>
  <div class="layout-all">
    <control :config="config" />
      <el-upload
        :action="get_upload_url()+model +'/UploadFile/upload'"
        :show-file-list="false"
        :on-success="handleAvatarSuccess_details"
        :headers="headers"
        :before-upload="beforeAvatarUpload"
        multiple
      >
        <el-button class="el-icon-picture" type="primary" >上传</el-button>
      </el-upload>
    <div class="layout-content">
      <el-table
        :data="tableData"
        stripe
        align="center"
        highlight-current-row
        style="width: 100%; margin-top: 50px;"
      >
        <el-table-column align="center" prop="id" label="序号" width="70" :fixed="isMobile()?'left':undefined"/>
        <el-table-column align="center" prop="original_name" label="原名" show-overflow-tooltip>
          <template slot-scope="scope">
            <span class="shen-he el-icon-circle-check" v-if="last_id>0&&scope.row.id>last_id"></span>
            {{scope.row.original_name}}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="file_size" label="图片" width="100" >
        <template slot-scope="scope">
          <pre-view :src="scope.row.url" :auto="true"/>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="file_size" label="大小" width="100" >
        <template slot-scope="scope">
            {{Math.round(scope.row.file_size/1024)}}kb
          </template>
        </el-table-column>
        <el-table-column align="center" width="160" prop="create_time" label="上传时间"/>
        <el-table-column
          align="center"
          prop="telephone"
          :fixed="isMobile()?undefined:'right'"
          width="180"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button type="success" icon="el-icon-check"  @click="onClick(scope.row)" circle></el-button>
            <el-button type="danger" icon="el-icon-delete"  @click="onDel(scope.row.id)" circle></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      :current-page="current_page"
      :page-size="per_page"
      layout="total, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import control from "@/components/control/control";
export default {
  name:'PictureLibrary',
  components: { control },
  props: { 
    isUpload:{
      type:Boolean,
      default:false,
    } 
  },
  computed:{
    model(){
      return this.$store.state.user.model
    },
    headers(){
      return {passport:this.$store.state.admin.token}
    },
  },
  data() {
    return {
      config: [
        {
          type: "text",
          placeholder: "请输入文件名",
          field: "original_name",
          val: "",
        },
      ],
      searchWhere: {
        title: "",
      },
      tableData: [],
      total: 0,
      thread: 0,
      last_id: 0,
      current_page: 1,
      per_page: 14,
    };
  },
  mounted() {
    this.onSearch();
  },
  methods: {
    // 查询
    handleSizeChange(val) {
      this.onSearch();
    },
    handleCurrentChange(val) {
      this.current_page = val;
      this.onSearch();
    },
    onSearch() {
      const form = {};
      form.page = this.current_page;
      form.map = this.searchWhere;
      this.get("/"+this.model+"/UploadFile/index", form, "POST").then((e) => {
        this.tableData = e.list.data;
        this.per_page = e.list.per_page;
        this.total = e.list.total;
        this.current_page = e.list.current_page;
      });
    },
    onClick(row){
      this.$msg_ok("success", "选择成功");
      this.$emit('onClick',row.url)
    },
    handleAvatarSuccess_details(file) {
      if(this.thread==0){
        this.thread=setTimeout(()=>{
          this.thread=0
          this.current_page=0;
          this.searchWhere={};
          if(this.tableData.length>0){
            this.last_id=this.tableData[0].id
          }
          this.onSearch()
          this.$msg_ok("success", "上传成功");
        },200)
      }
    },

    beforeAvatarUpload(file) {
      return this.$beforeAvatarUpload(file,2048)
    },
    onDel(id) {
      this.$msg_confirm().then((e) => {
        this.get("/"+this.model+"/UploadFile/del?id=" + id, "", "POST").then((res) => {
          this.onSearch();
        });
      });
    },
  },
};
</script>

<style>
.layout-all {
  margin-top: 20px;
  margin-left: 10px;
}
.layout-content {
  margin-top: 20px;
}
.shen-he {
    position: absolute;
    right: 4px;
    top: 13px;
    font-size: 17px;
    z-index: 10;
    border-radius: 100%;
    height: 25px;
    line-height: 17px;
    text-align: center;
    color: rgb(255 0 0 / 68%);
    padding: 5px;
}
</style>>
